package com.wpam.scrabble.mobile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

public class LetterManager {

	private StringBuilder letterBag_;
	private HashMap<String, String> playersLetters_;
	
	public LetterManager(ArrayList<String> playerNames) {
		
		initializeLetterBag();
		playersLetters_ = new HashMap<String, String>(playerNames.size());
		for (String name : playerNames)
			playersLetters_.put(name, getLetters(7).toString());
	}
	
	public String getPlayersLetters(String name) {
		return playersLetters_.get(name);
	}
	
	private void initializeLetterBag() {
		
		letterBag_ = new StringBuilder();
		for (int i = 0; i < 8; ++i) {
			letterBag_.append('A');
			letterBag_.append('I');
		}
		for (int i = 0; i<7; ++i)
			letterBag_.append('E');
		for (int i = 0; i<6; ++i) 
			letterBag_.append('O');
		for (int i = 0; i<5; ++i) {
			letterBag_.append('N');
			letterBag_.append('Z');
		}
		for (int i = 0; i<4; ++i) {
			letterBag_.append('R');
			letterBag_.append('S');
			letterBag_.append('W');
			letterBag_.append('Y');
		}
		for (int i = 0; i<3; ++i) {
			letterBag_.append('C');
			letterBag_.append('D');
			letterBag_.append('K');
			letterBag_.append('L');
			letterBag_.append('M');
			letterBag_.append('P');
			letterBag_.append('T');
		}
		for (int i = 0; i<2; ++i) {
			letterBag_.append('B');
			letterBag_.append('F');
			letterBag_.append('G');
			letterBag_.append('H');
			letterBag_.append('J');
			letterBag_.append('Ł');
			letterBag_.append('U');
			letterBag_.append(' ');
		}
		letterBag_.append('Ą');
		letterBag_.append('Ć');
		letterBag_.append('Ę');
		letterBag_.append('Ń');
		letterBag_.append('Ó');
		letterBag_.append('Ś');
		letterBag_.append('Ź');
		letterBag_.append('Ż');
	}
	
	private void putLettersBack(String str) {
		letterBag_.append(str);
	}
	
	private StringBuilder getLetters(int num) {
		
		StringBuilder retVal = new StringBuilder();
		Random r = new Random();
		int pos = 0;
		for (int i = 0; i < Math.min(num, letterBag_.length()); ++i) {
			pos = r.nextInt(letterBag_.length());
			retVal.append(letterBag_.charAt(pos));
			letterBag_.deleteCharAt(pos);
		}
		return retVal;
	}
	
	public String getLettersOfPlayer(String playerName) {
		return playersLetters_.get(playerName);
	}
	
	public String changeLettersOfPlayer(String playerName, String lettersToChange) {

		StringBuilder ret = getLetters(lettersToChange.length());
		int len = ret.length();
		if (len < lettersToChange.length()) 
			ret.append(lettersToChange.substring(lettersToChange.length() - len));
		putLettersBack(lettersToChange.substring(0, len));
		String retVal = ret.toString();
		playersLetters_.put(playerName, retVal);
		return retVal;
	}
	
	public String updatePlayerLetters(String playerName, String lettersUsed) {
		return changeLettersOfPlayer(playerName, lettersUsed);
	}

	public String updatePlayerLetters(String playerName, ArrayList<MovePart> letters) {

		StringBuilder lettersUsed = new StringBuilder();
		for (MovePart letter : letters)
			lettersUsed.append(letter.getLetter());
		return changeLettersOfPlayer(playerName, lettersUsed.toString());
	}
	
	public static String getPoints(String letter) {
				
		if (letter.equals(""))
			return "";
		
		switch (letter.charAt(0)) {
		case 'A':
		case 'E':
		case 'I':
		case 'N':
		case 'O':
		case 'R':
		case 'S':
		case 'W':
		case 'Z':
			return "1";
		case 'C':
		case 'D':
		case 'K':
		case 'L':
		case 'M':
		case 'P':
		case 'T':
		case 'Y':
			return "2";
		case 'B':
		case 'G':
		case 'H':
		case 'J':
		case 'Ł':
		case 'U':
			return "3";
		case 'F':
			return "4";
		case 'Ą':
		case 'Ę':
		case 'Ś':
		case 'Ż':
		case 'Ó':
			return "5";
		case 'Ć':
			return "6";
		case 'Ń':
		case 'Ź':
			return "7";
		default:
			return "";
		}
	}
}
